﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Management_Bed : System.Web.UI.UserControl
{

    public String SelectedValue
    {
        get
        {
            return DropDownList1.SelectedValue;
        }
    }

    public void loadBedByRoomID(int IDRoom)
    {
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "SELECT tbBed.ID, tbBed.[BedName] FROM tbBed WHERE IDRoom=" + IDRoom;
        SqlCommand cmd = new SqlCommand(sql, con);        
        SqlDataReader reader = cmd.ExecuteReader();
        DropDownList1.DataSource = reader;
        DropDownList1.DataTextField = "BedName";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
        con.Close();
    }

    public void loadFreeBedByRoomID(int IDRoom)
    {
        SqlConnection con = new SqlConnection(GlobalConstant.CONNECTION_STRING);
        con.Open();
        string sql = "SELECT tbBed.ID, tbBed.[BedName] FROM tbBed LEFT OUTER JOIN tbImpatient ON tbImpatient.[IDBed]=tbBed.[ID] LEFT OUTER JOIN tbPatient ON (tbImpatient.[IDPatient]=tbPatient.[ID] AND tbImpatient.[IDAppointment]=tbPatient.[LastAppointment] AND Status=0) LEFT OUTER JOIN tbRoom ON tbBed.[IDRoom]=tbRoom.[ID] WHERE tbRoom.ID=@IDRoom AND tbImpatient.IDBed IS NULL;";
        SqlCommand cmd = new SqlCommand(sql, con);
        cmd.Parameters.Add("IDRoom", System.Data.SqlDbType.Int).Value = IDRoom;
        SqlDataReader reader = cmd.ExecuteReader();
        DropDownList1.DataSource = reader;
        DropDownList1.DataTextField = "BedName";
        DropDownList1.DataValueField = "ID";
        DropDownList1.DataBind();
        con.Close();
    }
}